package com.bankLogisticsSys.controller;

import java.io.File;
import java.sql.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.bankLogisticsSys.service.ManageReportService;
import com.bankLogisticsSys.util.ExcelExportUtil;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

public class ManageReportController extends Controller {
	
	ManageReportService mrservice = new ManageReportService();
	
	public void list(){
		render("/WEB-INF/content/report/list.html");
	}
	
	public void reportList(){
		int bank = getSessionAttr("bank");
		java.util.Date date = getParaToDate("searchDate",new java.util.Date(System.currentTimeMillis()));
		Date searchDate = new Date(date.getTime());
		String network = getPara("network");
		List<Record> reportList = mrservice.getReportList2(bank, network, searchDate);
		int totalCount = reportList.size();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", totalCount);
		map.put("rows", reportList);
		renderJson(map);
	}
	
	//注，这里的2表示交通银行
	public void export(){
		java.util.Date date = getParaToDate("searchDate",new java.util.Date(System.currentTimeMillis()));
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		int first = c.getActualMinimum(c.DAY_OF_MONTH);    //获取本月最小天数
		int last = c.getActualMaximum(c.DAY_OF_MONTH);    //获取本月最大天数
		Map<String, String> titleData = new LinkedHashMap<String, String>();//标题，后面用到
        titleData.put("network", "银行网点");
        titleData.put("pack", "封包数");
        titleData.put("get", "揽收数");
        titleData.put("receive", "接收数");
        titleData.put("accept", "验收数");
		List<Record> monthReportList1 = mrservice.getMonthReportList(2, year, month);
		List<Record> monthReportList2 = mrservice.getMonthTotalList(2, year, month);
	    File file = new File(ExcelExportUtil.getMonthReportTitle(month, "交通银行"));
	    file = ExcelExportUtil.saveMonthReport(titleData,year, month,first,last, monthReportList1,monthReportList2, file);
	    this.renderFile(file);
	}
	
	public void exportDynamic(){
		java.util.Date startDate = getParaToDate("startDate",new java.util.Date(System.currentTimeMillis()));
		java.util.Date endDate = getParaToDate("endDate",new java.util.Date(System.currentTimeMillis()));
		Calendar start = Calendar.getInstance();
		Calendar end = Calendar.getInstance();
		start.setTime(startDate);
		end.setTime(endDate);
		int year = start.get(Calendar.YEAR);			//获取年
		int month = start.get(Calendar.MONTH) + 1;		//获取月
		int startDay = start.get(Calendar.DAY_OF_MONTH);//获取起始日
		int endDay = end.get(Calendar.DAY_OF_MONTH);	//获取截至日
		Map<String, String> titleData = new LinkedHashMap<String, String>();//标题，后面用到
        titleData.put("network", "银行网点");
        titleData.put("pack", "封包数");
        titleData.put("get", "揽收数");
        titleData.put("receive", "接收数");
        titleData.put("accept", "验收数");
		List<Record> monthReportList1 = mrservice.getMonthReportList(2, year, month);
		List<Record> monthReportList2 = mrservice.getMonthTotalList(2, year, month);
		String title = getPara("excelTitle","交通银行"+ month + "月统计报表")+".xls";
		File file = new File(title);
	    file = ExcelExportUtil.saveMonthReport(titleData,year, month,startDay,endDay, monthReportList1,monthReportList2, file);
	    this.renderFile(file);
	}
}
